﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using RemoteTestRunner.BusinessLogic.Views;
using RemoteTestRunner.Contracts;
using System.Windows.Data;
using RemoteTestRunner.BusinessLogic;
using RemoteTestRunner.BusinessLogic.ViewModels;

namespace RemoteTestRunner.FormsUI.Wpf
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Runner : Window, ITestRunnerWpfView
    {
        public Runner()
        {
            InitializeComponent();

            this.Loaded += new RoutedEventHandler(Runner_Loaded);
        }

        void Runner_Loaded(object sender, RoutedEventArgs e)
        {
            //cboProjects.SelectionChanged += new SelectionChangedEventHandler(cboProjects_SelectionChanged);
            //cboTests.SelectionChanged += new SelectionChangedEventHandler(cboTests_SelectionChanged);
        }

        #region ITestRunnerView Members

        //public TestResultSummary TestResultSummary
        //{
        //    get
        //    {
        //        System.ComponentModel.ICollectionView view;
        //        view = CollectionViewSource.GetDefaultView(lstTestRunHistory.ItemsSource);
        //        if (view != null)
        //        {
        //            return view.CurrentItem as TestResultSummary;
        //        }
        //        return null;
        //    }
        //    set
        //    {
        //        System.ComponentModel.ICollectionView view;
        //        view = CollectionViewSource.GetDefaultView(lstTestRunHistory.ItemsSource);
        //        view.MoveCurrentTo(value);
        //    }
        //}


        public void DisplayInvalidResultsFileMessage()
        {
            throw new NotImplementedException();
        }


        public void SetTestClassAndMethods(IList<TestClass> classesAndMethods)
        {
            //tvwClassesAndMethods.ItemsSource = classesAndMethods;
            //tvwClassesAndMethods.DisplayMemberPath = "Name";

            tvwClassesAndMethods.Items.Clear();

            //"All";
            TreeViewItem rootNode = NewTreeViewItem("All", GetBmpImage("All"), true);
            tvwClassesAndMethods.Items.Add(rootNode);

            if (classesAndMethods != null)
            {
                // Add the hierarchial classes and methods
                foreach (var testClass in classesAndMethods)
                {

                    //NewTreeViewItem(type.Name, null); 


                    //string uri = @"pack://application:,,,/WpfFormsUI;component/resources/All.bmp";



                    TreeViewItem classNode = NewTreeViewItem(testClass.Name, GetBmpImage("Class"), testClass.IsTestable);
                    //CheckBox classCheck = new CheckBox();
                    //classCheck.Content = type.Name;
                    //classNode.Header = classCheck;

                    rootNode.Items.Add(classNode);
                    //classNode.ImageKey = "Class";
                    //classNode.SelectedImageKey = "Class";
                    foreach (var testMethod in testClass.TestMethods)
                    {
                        //TreeViewItem methodNode = new TreeViewItem();
                        //CheckBox methodCheck = new CheckBox();
                        //methodCheck.Content = testMethod.Name;
                        //methodNode.Header = methodCheck;
                        //classNode.Items.Add(methodNode);

                        if (testMethod.DataDriven)
                        {

                            TreeViewItem methodNode = NewTreeViewItem(testMethod.Name, GetPngImage("DataDrivenTest"), testMethod.IsTestable);
                            classNode.Items.Add(methodNode);


                            //methodNode.ImageKey = "DataDriven";
                            //methodNode.SelectedImageKey = "DataDriven";
                        }
                        else
                        {
                            TreeViewItem methodNode = NewTreeViewItem(testMethod.Name, GetPngImage("Test"), testMethod.IsTestable);
                            classNode.Items.Add(methodNode);
                            //methodNode.ImageKey = "Method";
                            //methodNode.SelectedImageKey = "Method";
                        }
                    }
                }
            }

        }

        private TreeViewItem NewTreeViewItem(string text, Image image, bool isTestable)
        {
            TreeViewItem result = new TreeViewItem();
            //result.Margin = new Thickness (0);
            //result.Padding = new Thickness(0);
            //result.Focusable = false;

            Label checkText = new Label();
            checkText.Content = text;
            //checkText.Margin = new Thickness(0);
            checkText.Padding = new Thickness(2);

            StackPanel panel = new StackPanel();
            panel.Orientation = Orientation.Horizontal;
            //panel.Height = 16;
            //panel.Margin = new Thickness(0);

            panel.Children.Add(image);
            panel.Children.Add(checkText);

            CheckBox itemCheck = new CheckBox();
            itemCheck.Content = panel;
            itemCheck.IsEnabled = isTestable;
            //itemCheck.Margin = new Thickness(0);

            result.Header = itemCheck;

            return result;
        }

        private Image GetPngImage(string name)
        {
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            Uri uri = new Uri(string.Format("Images/{0}.png", name), UriKind.Relative);
            image.Source = new BitmapImage(uri);
            //System.Windows.Media.Imaging.PngBitmapDecoder 
            return image;
        }

        private Image GetBmpImage(string name)
        {
            System.Windows.Controls.Image image = new System.Windows.Controls.Image();
            Uri uri = new Uri(string.Format("Images/{0}.bmp", name), UriKind.Relative);
            image.Source = new BitmapImage(uri);
            return image;
        }

        //public void DisplayTestResults(TestResult result)
        //{
        //    pnlTestResults.DataContext = result;

        //    System.ComponentModel.ICollectionView view;
        //    view = CollectionViewSource.GetDefaultView(lvwTestResultListView.ItemsSource);

        //    if (view != null)
        //    {
        //        view.GroupDescriptions.Add(new PropertyGroupDescription("Outcome"));
        //    }

        //    //throw new NotImplementedException();
        //}

        public bool CanDeleteSelectedResults()
        {
            throw new NotImplementedException();
        }

        #endregion

        public void SetRootDataContext(object context)
        {
            this.DataContext = context;
        }

        private void ShowProjectAdmin(object sender, RoutedEventArgs e)
        {
            var admin = IoC.Container.Resolve<ProjectAdmin>();
            admin.DataContext = IoC.Container.Resolve<ProjectAdminViewModel>();
            admin.ShowDialog();
        }

        #region ITestRunnerWpfView Members


        public void SetDetailedResults(string results)
        {
            detailedTestResults.NavigateToString(results);
        }

        #endregion
    }
}
